package com.resource.manager.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.resource.manager.search.vo.FileEntity;
import com.resource.manager.search.vo.FileTypeEntity;
import com.resource.manager.search.vo.MainSearchEntity;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class FileSearchUtil {

    /**
     * 查询文件
     */
    public static void searchFile(MainSearchEntity entity) {
        String path = entity.getPath();
        List<FileTypeEntity> fileTypeEntitys = entity.getFileTypeEntitys();
        fileTypeEntitys.forEach(f -> f.setSearchSuccess(false));
        CountDownLatch countDownLatch = new CountDownLatch(fileTypeEntitys.size());
        for (FileTypeEntity typeEntity : fileTypeEntitys) {
            new Thread(() -> {
                searchFile(typeEntity, path);
                countDownLatch.countDown();
            }).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        entity.setSearchFlag(true);
    }

    private static void searchFile(FileTypeEntity typeEntity, String path) {
        List<String> suffix = typeEntity.getSuffix();
        File root = new File(path);
        //获取目录已用的空间
        List<File> files = new ArrayList<>();
        findAllFile(root, suffix, files);

        //设置文件
        List<FileEntity> fileEntities = new ArrayList<>();
        for (File file : files) {
            FileEntity fileEntity = new FileEntity();
            fileEntity.setFileName(file.getName());
            fileEntity.setPath(file.getAbsolutePath());
            fileEntity.setTypePath(typeEntity.getIco());
            fileEntity.setSize(file.length());
            //获取文件创建日期
            fileEntity.setLastModify(DateUtil.format(new Date(file.lastModified()), "yyyy-MM-dd HH:mm:ss"));
            fileEntity.setLastModifyTime(file.lastModified());
            fileEntities.add(fileEntity);
        }
        fileEntities.sort((o1, o2) -> o2.getLastModifyTime().compareTo(o1.getLastModifyTime()));
        typeEntity.setFileList(fileEntities);
        typeEntity.setCount(fileEntities.size());
        typeEntity.setSearchSuccess(true);
    }

    private static void findAllFile(File root, List<String> suffix, List<File> allFile) {
        //递归查询所有文件
        if (root.isDirectory()) {
            File[] files = root.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        findAllFile(file, suffix, allFile);
                    } else {
                        for (String s : suffix) {
                            if (file.getName().endsWith(s) || file.getName().toUpperCase().endsWith(s.toUpperCase())) {
                                allFile.add(file);
                            }
                        }
                    }
                }
            }
        }
    }

    public static void export(List<FileTypeEntity> exportList,String path){
        File file = new File(path);
        if(!file.exists()){
            boolean mkdir = file.mkdir();
            if(!mkdir){
                return;
            }
        }
        exportList.forEach(e -> {
            File file1 = new File(file.getAbsolutePath() + "\\" + e.getName());
            if(!file1.exists()) {
                file1.mkdir();
            }
            e.getFileList().forEach(f -> {
                File file2 = new File(file1.getAbsolutePath() + "\\" + f.getFileName());
                try {
                    if(!file2.exists()){
                        FileUtil.copy(f.getPath(), file2.getAbsolutePath(), true);
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            });
        });
    }

    public static void main(String[] args) {
        MainSearchEntity mainSearchEntity = new MainSearchEntity();
        mainSearchEntity.setPath("D:\\");
        FileTypeEntity fileTypeEntity = new FileTypeEntity();
        fileTypeEntity.setSuffix(Arrays.asList(".png", ".jpg"));
        mainSearchEntity.setFileTypeEntitys(Arrays.asList(fileTypeEntity));
        searchFile(mainSearchEntity);
        System.out.println();
    }
}
